/* 
 * Blobscanner Processing library 
 * by Antonio Molinaro - 01/01/2011.
 * Computes the number of blobs in the video frame.
 *
 */
import Blobscanner.*;
import processing.video.*;

Detector bd;
Capture frame;
 

void setup(){
  size(320, 240);
  frame = new Capture(this, width, height);
  bd = new Detector( this, 0, 0, frame.width, frame.height, 255 );
 
 frame.start();
}

void draw(){
   if(frame.available()){
    frame.read();
    image(frame, 0, 0);
  }
   
  
  frame.filter(THRESHOLD);
  
      image(frame, 0, 0);

  
  bd.imageFindBlobs(frame);
  
   
  
  println(bd.getBlobsNumber() + " BLOBS FOUND");
  
  
    //This call is indispensable for nearly all the other methods
  //(please check javadoc).
  bd.loadBlobsFeatures();

  //This methods needs to be called before to call 
  //findCentroids(booleaan, boolean) methods.
  bd.weightBlobs(false);

  //Computes the blob center of mass. If the first argument is true, 
  //prints the center of mass coordinates x y to the console. If the 
  //second argument is true, draws a point at the center of mass coordinates.
  bd.findCentroids(false, false);


  stroke(255, 100, 0); 

  //For each blob in the image..
  for (int i = 0; i < bd.getBlobsNumber(); i++) {

    stroke(0, 255, 0);
    strokeWeight(5);

    //...computes and prints the centroid coordinates x y to the console...
    println("BLOB " + (i+1) + " CENTROID X COORDINATE IS " + bd.getCentroidX(i));
    println("BLOB " + (i+1) + " CENTROID Y COORDINATE IS " + bd.getCentroidY(i));
    println("\n");

    //...and draws a point to their location. 
    point(bd.getCentroidX(i), bd.getCentroidY(i));

    //Write coordinate to the screen.
    fill(255, 0, 0);
    text("x-> " + bd.getCentroidX(i) + "\n" + "y-> " + bd.getCentroidY(i), bd.getCentroidX(i), bd.getCentroidY(i)-7);
  }
  
  
}
 
